package com.app.studyroom;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.studyroom.adapter.BookingManageAdapter;
import com.app.studyroom.api.ApiClient;
import com.app.studyroom.api.ApiService;
import com.app.studyroom.api.model.ssrBooking.BookingItem;
import com.app.studyroom.api.model.ssrBooking.BookingPageRequest;
import com.app.studyroom.api.model.ssrBooking.BookingPageResponse;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import android.util.Log;

public class AdminBookingControlActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private BookingManageAdapter adapter;
    private List<BookingItem> bookingList = new ArrayList<>();
    private ApiService apiService;
    private ImageView ic_back;
    private String token;
    private int currentPage = 1;
    private final int pageSize = 11;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booking_control);

        // 获取 token
        token = getSharedPreferences("Session", MODE_PRIVATE).getString("token", "");
        apiService = ApiClient.getClient().create(ApiService.class);

        // 初始化 RecyclerView
        recyclerView = findViewById(R.id.seat_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BookingManageAdapter(bookingList);
        recyclerView.setAdapter(adapter);

        // 返回键
        ic_back = findViewById(R.id.ic_back);
        ic_back.setOnClickListener(v -> onBackPressed());

        // 加载列表
        loadBookingList();

        //翻页
        Button btnPrev = findViewById(R.id.btnPrevPage);
        Button btnNext = findViewById(R.id.btnNextPage);
        btnPrev.setOnClickListener(v -> {
            if (currentPage > 1) {
                currentPage--;
                loadBookingList();
            } else {
                Toast.makeText(this, "已经是第一页", Toast.LENGTH_SHORT).show();
            }
        });
        btnNext.setOnClickListener(v -> {
            currentPage++;
            loadBookingList();
        });

    }

    private void loadBookingList() {
        BookingPageRequest request = new BookingPageRequest(currentPage, pageSize);

        apiService.pageBookingList(token, request).enqueue(new Callback<BookingPageResponse>() {
            @Override
            public void onResponse(Call<BookingPageResponse> call, Response<BookingPageResponse> response) {
                if (response.isSuccessful() && response.body() != null && response.body().getCode() == 200) {
                    List<BookingItem> items = response.body().getData().getData();
                    TextView tvPage = findViewById(R.id.tvPageIndicator);
                    Button btnPrev = findViewById(R.id.btnPrevPage);
                    Button btnNext = findViewById(R.id.btnNextPage);

                    // 设置页码
                    tvPage.setText("第 " + currentPage + " 页");
                    btnPrev.setEnabled(currentPage > 1);
                    btnNext.setEnabled(items != null && items.size() >= pageSize);

                    if (items == null || items.isEmpty()) {
                        Toast.makeText(AdminBookingControlActivity.this, "暂无预约数据", Toast.LENGTH_SHORT).show();

                        // 自动回退页码
                        if (currentPage > 1) {
                            currentPage--;
                        }
                        return;
                    }

                    bookingList = items;
                    adapter.setBookingList(bookingList);
                    Log.d("预约列表返回", "数量: " + (items != null ? items.size() : "null"));

                    if (items == null || items.isEmpty()) {
                        Toast.makeText(AdminBookingControlActivity.this, "当前没有预约数据", Toast.LENGTH_SHORT).show();
                    } else {
                        bookingList = items;
                        adapter.setBookingList(bookingList);
                        Toast.makeText(AdminBookingControlActivity.this, "预约数据加载成功：" + items.size() + " 条", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    String err = response.body() != null ? response.body().getMessage() : "无响应内容";
                    Toast.makeText(AdminBookingControlActivity.this, "获取失败：" + err, Toast.LENGTH_SHORT).show();
                    Log.e("预约加载失败", "code=" + response.code() + ", message=" + err);
                }
            }

            @Override
            public void onFailure(Call<BookingPageResponse> call, Throwable t) {
                Toast.makeText(AdminBookingControlActivity.this, "请求错误：" + t.getMessage(), Toast.LENGTH_SHORT).show();
                Log.e("预约列表异常", t.getMessage(), t);
            }
        });
    }

}
